package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import java.awt.Font;
import javax.swing.JTextPane;

import domain.Stu;
import service.Impl.StuServiceImpl;

import javax.swing.JButton;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class Statistics {

	private JFrame frame;
	//注入service
		private StuServiceImpl stuService=new StuServiceImpl();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Statistics window = new Statistics();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Statistics() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(Color.BLACK);
		frame.setBounds(100, 100, 570, 402);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Statistics.class.getResource("/img/9.jpg")));
		label.setBounds(-40, 190, 514, 274);
		frame.getContentPane().add(label);
		
		
		
		
		JLabel label_1 = new JLabel("课程统计");
		label_1.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1.setForeground(Color.WHITE);
		label_1.setBackground(Color.BLACK);
		label_1.setBounds(0, 39, 72, 34);
		frame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("个人统计");
		label_2.setBackground(Color.BLACK);
		label_2.setForeground(Color.WHITE);
		label_2.setFont(new Font("宋体", Font.PLAIN, 15));
		label_2.setBounds(0, 116, 72, 21);
		frame.getContentPane().add(label_2);
		
		JTextPane AvgCourse = new JTextPane();
		AvgCourse.setFont(new Font("宋体", Font.PLAIN, 15));
		AvgCourse.setForeground(Color.WHITE);
		AvgCourse.setBackground(Color.BLACK);
		AvgCourse.setBounds(219, 39, 62, 34);
		frame.getContentPane().add(AvgCourse);
		
		JTextPane AvgPerson = new JTextPane();
		AvgPerson.setFont(new Font("宋体", Font.PLAIN, 15));
		AvgPerson.setForeground(Color.WHITE);
		AvgPerson.setBackground(Color.BLACK);
		AvgPerson.setBounds(219, 109, 62, 34);
		frame.getContentPane().add(AvgPerson);
		
		JTextPane SumPerson = new JTextPane();
		SumPerson.setFont(new Font("宋体", Font.PLAIN, 15));
		SumPerson.setForeground(Color.WHITE);
		SumPerson.setBackground(Color.BLACK);
		SumPerson.setBounds(344, 109, 62, 34);
		frame.getContentPane().add(SumPerson);
		
		JTextPane MaxCourse = new JTextPane();
		MaxCourse.setFont(new Font("宋体", Font.PLAIN, 15));
		MaxCourse.setForeground(Color.WHITE);
		MaxCourse.setBackground(Color.BLACK);
		MaxCourse.setBounds(344, 39, 62, 34);
		frame.getContentPane().add(MaxCourse);
		
		JTextPane MinCourse = new JTextPane();
		MinCourse.setFont(new Font("宋体", Font.PLAIN, 15));
		MinCourse.setForeground(Color.WHITE);
		MinCourse.setBackground(Color.BLACK);
		MinCourse.setBounds(476, 39, 62, 34);
		frame.getContentPane().add(MinCourse);
		
		JLabel label_1_1 = new JLabel("平均分");
		label_1_1.setForeground(Color.WHITE);
		label_1_1.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1_1.setBackground(Color.BLACK);
		label_1_1.setBounds(166, 39, 45, 34);
		frame.getContentPane().add(label_1_1);
		
		JLabel label_1_1_1 = new JLabel("平均分");
		label_1_1_1.setForeground(Color.WHITE);
		label_1_1_1.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1_1_1.setBackground(Color.BLACK);
		label_1_1_1.setBounds(168, 109, 45, 34);
		frame.getContentPane().add(label_1_1_1);
		
		JLabel label_1_1_2 = new JLabel("最高分");
		label_1_1_2.setForeground(Color.WHITE);
		label_1_1_2.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1_1_2.setBackground(Color.BLACK);
		label_1_1_2.setBounds(295, 39, 45, 34);
		frame.getContentPane().add(label_1_1_2);
		
		JLabel label_1_1_3 = new JLabel("总分");
		label_1_1_3.setForeground(Color.WHITE);
		label_1_1_3.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1_1_3.setBackground(Color.BLACK);
		label_1_1_3.setBounds(295, 109, 45, 34);
		frame.getContentPane().add(label_1_1_3);
		
		JLabel label_1_1_4 = new JLabel("最低分");
		label_1_1_4.setForeground(Color.WHITE);
		label_1_1_4.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1_1_4.setBackground(Color.BLACK);
		label_1_1_4.setBounds(420, 39, 45, 34);
		frame.getContentPane().add(label_1_1_4);
		
		//课程下拉列表
		JComboBox CourseBox = new JComboBox();
		CourseBox.setFont(new Font("宋体", Font.PLAIN, 15));
		CourseBox.setForeground(Color.WHITE);
		CourseBox.setBackground(Color.BLACK);
		CourseBox.setBounds(65, 39, 102, 34);	
		List<String>Courses=stuService.findAllCourse();
		for(String c:Courses) {
			CourseBox.addItem(c);
		}
		CourseBox.addItemListener(new ItemListener() {
			//给下拉列表添加监听事件
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if(e.getStateChange() == ItemEvent.SELECTED){
//					System.out.println("选中" + e.getItem());
					String course=(String) e.getItem();
					String avg=stuService.findAvgCourse(course);
					String max=stuService.findMaxCourse(course);
					String min=stuService.findMinCourse(course);
					AvgCourse.setText(avg);
					MaxCourse.setText(avg);
					MinCourse.setText(avg);
//							textField_2.setText((String) e.getItem());				
				}			
			}
		});	
		frame.getContentPane().add(CourseBox);
		
		//学生下拉表
		JComboBox stuBox = new JComboBox();
		stuBox.setFont(new Font("宋体", Font.PLAIN, 15));
		stuBox.setForeground(Color.WHITE);
		stuBox.setBackground(Color.BLACK);
		stuBox.setBounds(65, 109, 102, 34);
		List<Stu>stus=stuService.findAllStu();
		for(Stu s:stus) {
			String str=s.getID()+" "+s.getName();
			stuBox.addItem(str);
		}
		stuBox.addItemListener(new ItemListener() {
			//给下拉列表添加监听事件
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if(e.getStateChange() == ItemEvent.SELECTED){
//					System.out.println("选中" + e.getItem());
					String str=(String) e.getItem();	
					int id=Integer.parseInt(str.split(" ")[0]);
					String avg=stuService.findAvgPerson(id);
					String sum=stuService.findSumPerson(id);
					AvgPerson.setText(avg);
					SumPerson.setText(sum);
				}			
			}
		});	
		
		frame.getContentPane().add(stuBox);
		
		
		JButton btnNewButton = new JButton("返回");
		btnNewButton.setBackground(Color.BLACK);
		btnNewButton.setForeground(Color.WHITE);
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		btnNewButton.setBounds(466, 308, 72, 34);
		frame.getContentPane().add(btnNewButton);
		
	}
}
